package com.example.cooldemo;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.cool_core.config.PublicConfig;
import com.example.cool_core.config.RefreshUtil;
import com.example.cool_core.listener.OnErrorOfLoadingView;
import com.example.cool_core.listener.OnHttpRefreshListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;


/**
 * 目前的功能
 * 配置一些公共ui
 * 封装了eventbus
 *
 * @author liudong
 * @date 2017/11/10
 */

public abstract class BaseActivity extends AppCompatActivity implements OnHttpRefreshListener {
    protected BaseActivity mActivity;
    protected PublicConfig mPublicConfig;
    protected RefreshUtil mRefreshUtil;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        mRefreshUtil = new RefreshUtil(this);
        setContentView(R.layout.cool_root);
        //装载内容布局
        LayoutInflater.from(mActivity).inflate(setLayoutId(savedInstanceState), (FrameLayout) findViewById(R.id.cool_boot_content));
        mPublicConfig = new PublicConfig(mActivity, LayoutInflater.from(mActivity), mRefreshUtil, new OnErrorOfLoadingView() {
            @Override
            public FrameLayout getBootView() {
                return findViewById(R.id.cool_boot);
            }

            @Override
            public FrameLayout getTopbarView() {
                return findViewById(R.id.cool_boot_topBar);
            }

            @Override
            public ViewGroup getLoadingLayouView() {
                return findViewById(BaseActivity.this.setLoadingRootLayout());
            }

            @Override
            public boolean isLoadingOrErrorView() {
                return isLoadingOrErrorLayout();
            }
        });
        //初始化
        init(savedInstanceState);
    }

    protected boolean isLoadingOrErrorLayout() {
        return true;
    }


    @IdRes
    protected int setLoadingRootLayout() {
        return 0;
    }

    protected void init(Bundle savedInstanceState) {

    }

    @LayoutRes
    protected abstract int setLayoutId(Bundle savedInstanceState);


    /**
     * 设置下拉刷新
     */
    protected void setRefresh(SmartRefreshLayout refreshLayout) {
        setRefresh(refreshLayout, false);
    }

    protected void setRefresh(SmartRefreshLayout refreshLayout, boolean isLoadMore) {
        mRefreshUtil.setRefresh(refreshLayout, isLoadMore);
    }

    //加载更多 也就是分页 设置数据
    protected void setRefreshData(BaseQuickAdapter adapter, List data) {
        mRefreshUtil.setRefreshData(adapter, data);
    }


    //下拉刷新 加载更多 请求会再这
    @Override
    public void onHttpRefresh(int page) {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}
